
<template src="./tpl.html"></template>
<script>
import {
  CONSENT_GET_CONSENT_LIST,
  CONSENT_DELETE,
  CONSENT_EDIT,
  CONSENT_GET_TEMPLATE_LIST,
  CONSENT_GET_FJ_LIST,
  CONSENT_GET_ZK_LIST
} from "_SERVICES/api";
import { trimAll } from "UTILS/string";
import { isvalidateNum2 } from "UTILS/validate";
import { timeFormatter,getDay } from "UTILS/format";

import { getStorage, setStorage } from "UTILS/util";

import page from "@/modules/CST/mxins/page";
import selectTemplate from "@/modules/CST/components/SelectTemplate";
export default {
  name: "consentList",
  mixins: [page],
  components: {
    selectTemplate,
  },
  data() {
    return {
      pickerOptions: {
        shortcuts: [{
          text: '最近一周',
          onClick(picker) {
            const end = new Date();
            const start = new Date();
            start.setTime(start.getTime() - 3600 * 1000 * 24 * 7);
            picker.$emit('pick', [start, end]);
          }
        }, {
          text: '最近一个月',
          onClick(picker) {
            const end = new Date();
            const start = new Date();
            start.setTime(start.getTime() - 3600 * 1000 * 24 * 30);
            picker.$emit('pick', [start, end]);
          }
        }, {
          text: '最近三个月',
          onClick(picker) {
            const end = new Date();
            const start = new Date();
            start.setTime(start.getTime() - 3600 * 1000 * 24 * 90);
            picker.$emit('pick', [start, end]);
          }
        }]
      },
      formInline: {
        ZYH: "",
        BRBH: "",
        BRXM: "",
        ZTBZ: "",
        TID: "",
        FJID: "",
        filter_zkid: "",
        QSSJ: [],
      },
      userInfo: {},
      queryInfo: {},

      headers: [
        {
          label: "编号",
          prop: "SID",
        },
        {
          label: "关联ID",
          prop: "GET_PATIENT_BY_PRIMARY",
        },
        {
          label: "姓名",
          prop: "BRXM",
        },
        {
          label: "就诊卡号",
          prop: "BRBH",
        },
        {
          label: "住院号",
          prop: "ZYH",
        },

        {
          label: "签署时间",
          prop: "XGSJ",
        },
        {
          label: "知情同意书名称",
          prop: "MC",
        },
        {
          label: "签署医护人员姓名",
          prop: "XGYHXM",
        },
        {
          label: "归属科室",
          prop: "ZKMC",
        },
      ],
      addShow: false,
      templateList: [],
      FJ_LIST: [],
      ZK_LIST: [],
      card_obj: {},

    };
  },
  watch: {
    $route: {
      handler(to, from) {
        let userInfo = getStorage("cst_userInfo");
        this.userInfo = userInfo ? JSON.parse(userInfo) : {};
        this.queryInfo = this.$route.query || {};
        this.formInline = {
          ZYH: this.queryInfo.ZYH || "",
          BRBH: this.queryInfo.BRBH || "",
          BRXM: this.queryInfo.BRXM || "",
        };

        if (!this.formInline.BRBH && getStorage("consentList_params")) {
          this.formInline = JSON.parse(getStorage("consentList_params"));
        }
        if (this.formInline.BRXM) {
          document.title = this.formInline.BRXM
        }

        this.getList();
      },
      immediate: true,
    },
  },
  beforeDestroy() {
    if (this.queryInfo.fromPage) {
      //如果页面带参数了，则不在本地保存查询信息

      setStorage({
        name: "consentList_params",
        value: JSON.stringify({}),
      });
    } else {
      setStorage({
        name: "consentList_params",
        value: JSON.stringify(this.formInline),
      });
    }
  },

  created() {
    //从病人列表页面过来的 都入参{
    // fromPage：PATIENT_LIST"
    // GET_PATIENT_BY_PRIMARY:"获取对应科室患者详细信息的主索引"
    // }
    // let query = this.$route.query || {}
    // this.queryInfo = query;
    this.getTemplateList();
    this.getFjList();
    this.get_zkid_list()
  },
  mounted() { },
  methods: {
    resetForm(name) {
      this.formInline = {
        ZYH: "",
        BRBH: "",
        BRXM: "",
        ZTBZ: "",
        TID: "",
        FJID: "",
        filter_zkid: "",
        QSSJ:[]

      };
      this.$refs.ybCard.clear();
      this.card_obj = {}
      this.search();
    },
    //刷卡只受预约时间的约束
    back(obj) {
      if (obj.code == 0) {
        this.card_obj = obj;
        this.search();
      } else {
        this.card_obj = {}
        if (obj.msg) {
          this.$message.error(obj.msg)
        }
      }

    },
    search_2() {
      if (this.$refs.ybCard) {
        this.$refs.ybCard.clear();
      }
      this.card_obj = {}
      this.search()
    },

    get_zkid_list() {
      CONSENT_GET_ZK_LIST({

      }).then((res) => {
        console.log("获取专科列表成功")
        this.ZK_LIST = res.data || []
      })
    },
    getTemplateList() {
      CONSENT_GET_TEMPLATE_LIST({
        ZTBZ: 'EXCEPT_0',
        download: 1,
      }).then((res) => {
        this.templateList = res.data || [];
      });
    },
    getFjList() {
      CONSENT_GET_FJ_LIST({}).then((res) => {
        this.FJ_LIST = res.data || [];
        console.log(res);
      });
    },

    seeRow(row) {
      let url = `${process.env.VUE_APP_BASE_API}router/showpdf?SID=${row.SID}&BRBH=${row.BRBH}`;
      window.open(url);
    },
    editRow(row) {
      this.$router.push({
        path: "/consentTemplate",
        query: {
          CONSENT_ID: row.SID,
          TYPE: "2",
        },
      });
    },
    deleteRow(row) {
      this.$confirm("此操作将永久删除该文件, 是否继续?", "提示", {
        confirmButtonText: "确定",
        cancelButtonText: "取消",
        type: "warning",
      })
        .then(() => {
          CONSENT_DELETE({
            SID: row.SID,
          }).then((res) => {
            this.$message.success("删除成功");
            this.getList();
          });
        })
        .catch(() => { });
    },
    addConsent() {
      if (!this.queryInfo.GET_PATIENT_BY_PRIMARY) {
        this.$message.error(
          "缺少获取患者信息的唯一主索引，无法添加同意书，请从病人预约列表重新进入"
        );
        return false;
      }
      this.addShow = true;
    },
    selectTemp(row) {
      console.log(row);
      this.addShow = false;
      this.$router.push({
        path: "/consentTemplate",
        query: {
          GET_PATIENT_BY_PRIMARY: this.queryInfo.GET_PATIENT_BY_PRIMARY,
          GET_PATIENT_TYPE: this.queryInfo.GET_PATIENT_TYPE,
          TID: row.TID,
          TYPE: "1",
        },
      });
    },

    getList() {
      this.listLoading = true;
      // let params = {
      //   ...this.formInline,
      // };
      // Object.assign(params, this.getParams());


      let params = {
        ...this.formInline,
        BSLX: this.card_obj.BSLX || "",//BSLX
        WYBS: this.card_obj.WYBS || "",//WYBS
      };
      if (params.WYBS) {
        params = {
          BSLX: params.BSLX,
          WYBS: params.WYBS
        }
      }
      Object.assign(params, this.getParams());




      return CONSENT_GET_CONSENT_LIST(params)
        .then((res) => {
          this.listLoading = false;
          this.listQuery.totalNum = res.totalNum;
          this.tableData = res.data;
          this.setBrbh(res.patientInfo)

          this.CONSENT_GET_CURRENT_VERSION();//检查版本
        })
        .catch((res) => {
          this.tableData = [];
          this.listLoading = false;
          this.downloadLoading = false;
      });
    },

  },
};
</script>

<style lang="less"></style>
